﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// 序列化扩展类[注:当对象中有继承后重写父类的属性时,不适用]
    /// </summary>
    public static partial class SerializeEx
    {
        #region JsonSerializer-JasonSerializer
        private static JsonSerializerOptions _camelOptions = null;
        private static JsonSerializerOptions _pascalOptions = null;

        private class JsonSerializerMemberCamelNamePolicy : JsonNamingPolicy
        {
            public JsonSerializerMemberCamelNamePolicy()
                : base()
            {

            }

            public override string ConvertName(string name)
            {
                if (char.IsUpper(name[0]))
                {
                    if (string.Equals(name, "ID"))
                    {
                        return name.ToLower();
                    }
                    else
                    {
                        char ch = char.ToLower(name[0]);
                        name = $"{ch}{name.Substring(1)}";
                    }
                }

                return name;
            }
        }

        /// <summary>
        /// 获取序列化策略选项
        /// </summary>
        /// <param name="memberCamelName">[true:成员使用Camel命名法（驼峰命名法）;false:成员使用Pascal命名法（帕斯卡命名法）]</param>
        /// <returns>列化策略</returns>
        private static JsonSerializerOptions GetJsonSerializerOptions(bool memberCamelName)
        {
            JsonSerializerOptions options;
            if (memberCamelName)
            {
                if (_camelOptions == null)
                {
                    _camelOptions = new JsonSerializerOptions()
                    {
                        PropertyNamingPolicy = new JsonSerializerMemberCamelNamePolicy()
                    };
                }

                options = _camelOptions;
            }
            else
            {
                if (_pascalOptions == null)
                {
                    _pascalOptions = new JsonSerializerOptions()
                    {
                        //PropertyNamingPolicy = new JsonSerializerMemberCamelNamePolicy()
                    };
                }

                options = _pascalOptions;
            }

            return options;
        }

        /// <summary>
        /// JSON序列化
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <param name="memberCamelName">[true:成员使用Camel命名法（驼峰命名法）;false:成员使用Pascal命名法（帕斯卡命名法）]</param>
        /// <returns>json序列化之后的字符串</returns>
        public static string JsonSerializerObject(object obj, bool memberCamelName = true)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            JsonSerializerOptions jsonSerializerOptions = GetJsonSerializerOptions(memberCamelName);
            return JsonSerializer.Serialize(obj, obj.GetType(), jsonSerializerOptions);
        }

        /// <summary>
        /// JSON反序列化
        /// </summary>
        /// <typeparam name="T">反序列化之类的类型</typeparam>
        /// <param name="json">待反序列化的json字符串</param>
        /// <param name="memberCamelName">[true:成员使用Camel命名法（驼峰命名法）;false:成员使用Pascal命名法（帕斯卡命名法）]</param>
        /// <returns>反序列化之后的对象</returns>
        public static T JsonDeserializeObject<T>(string json, bool memberCamelName = true)
        {
            if (string.IsNullOrEmpty(json))
            {
                throw new ArgumentNullException(nameof(json));
            }

            JsonSerializerOptions jsonSerializerOptions = GetJsonSerializerOptions(memberCamelName);
            return JsonSerializer.Deserialize<T>(json, jsonSerializerOptions);
        }

        /// <summary>
        /// JSON反序列化
        /// </summary>
        /// <param name="json">待反序列化的json字符串</param>
        /// <param name="targetType">反序列化之类的类型</param>
        /// <param name="memberCamelName">[true:成员使用Camel命名法（驼峰命名法）;false:成员使用Pascal命名法（帕斯卡命名法）]</param>
        /// <returns>反序列化之后的对象</returns>
        public static object JsonDeserializeObject(string json, Type targetType, bool memberCamelName = true)
        {
            if (string.IsNullOrEmpty(json))
            {
                throw new ArgumentNullException(nameof(json));
            }

            JsonSerializerOptions jsonSerializerOptions = GetJsonSerializerOptions(memberCamelName);
            return JsonSerializer.Deserialize(json, targetType, jsonSerializerOptions);
        }
        #endregion
    }


}
